#include <iostream>
using namespace std;

#include <memory>
#include <vector>

#include "chair.h"


void useRegularPointersforObjectCreation (const int numberOfObjects , bool isDeleteCalled)
{
	Chair *ptr_arr = new Chair[numberOfObjects];
	cout << "Total Objects Created : " << Chair::created << endl;
	
	if ( isDeleteCalled )
	{
		delete [] ptr_arr;
	}
}

void useAutoPointersforObjectCreation (const int numberOfObjects)
{
	auto_ptr< vector<Chair> > auto_ptr_arr( new vector<Chair> (numberOfObjects) ); 
	cout << "Total Objects Created : " << Chair::created << endl;
}

void printHeadingSection(bool isAutoPointerUsed , bool isDeleteUsed , int numberOfObjects )
{
	cout << "=================================================================================" << endl;
	
	if ( isAutoPointerUsed){
		cout <<  "Pointer Type : Auto Pointer " << endl; 
		cout <<  "Target Number of Objects: " << numberOfObjects << endl;
	}else{
		cout <<  "Pointer Type : Regular Pointer " << endl;
		cout <<  "Target Number of Objects: " << numberOfObjects << endl;
		
		if ( isDeleteUsed ){
			cout <<  "Explicit Destruction of Objects (using delete keyword) : true " << endl;
		}else{
			cout <<  "Explicit Destruction of Objects (using delete keyword) : false " << endl;
		}
	}
	cout << "=================================================================================" << endl;

}

void printUsage()
{
	cout << "=================================================================================" << endl;
	cout << "Usage: Garbage.exe <ptr_type> <number_of_objects> [exiplicit_usage_of_delete]" << endl;
	cout << "Examples:" << endl;
	cout << " Garbage.exe auto_ptr 10" << endl;
	cout << " Garbage.exe reg_ptr 10 use_delete" << endl;
	cout << " Garbage.exe reg_ptr 10" << endl;
	cout << "=================================================================================" << endl;

}

int main( int argc, char *argv[] )
{	
	bool isAutoPointerUsed;
	bool isDeleteCalled;
	int noOfObjects = 0;
	

	if ( argc < 3 )
	{
		printUsage();
		return -1;
	}else{
		
		if ( strcmp (argv[1] , "auto_ptr") == 0 )
		{
			isAutoPointerUsed = true;
		}else if ( strcmp (argv[1] , "reg_ptr") == 0 )
		{
			isAutoPointerUsed = false;
		}	
		noOfObjects = atoi(argv[2]);

		if ( argc == 4 && ( strcmp (argv[3], "use_delete" ) == 0 ) )
		{
			isDeleteCalled = true;
		}else{
			isDeleteCalled = false;
		}

	}

	const int numberOfObjects = noOfObjects;

	printHeadingSection(isAutoPointerUsed,isDeleteCalled, numberOfObjects);
	cout << "==> Start of Execution <==" << endl;
	
	if ( isAutoPointerUsed )
	{
		useAutoPointersforObjectCreation(numberOfObjects);
	}else{
		useRegularPointersforObjectCreation(numberOfObjects, isDeleteCalled );
	}
	cout << "Total Objects Destroyed : " << Chair::destroyed << endl;
	cout << "== > End of Main Procedure <==" << endl;
    return 0;
}


